$(function () {
    getinfo(1);
    $("#filter").click(function () {
       getinfo(1);
    })
})
function getinfo(id) {
    var title=$("#title").val();
    var categories=$("#categories").val();
    $.post({
        url: baseurl+api.article.manage+id,
        data:{title: title,categoriesname: categories},
        xhrFields: {
            withCredentials: true
        },
        success: function (res) {
            var s1="";
            var s2 = "";
            if(res.code=="200"){
                var articleinfo = res.data.articleinfo;
                for (var i = 0; i < articleinfo.length; i++) {
                    s1+=" <tr>\n" +
                        "                            <td>"+articleinfo[i].id+"</td>\n" +
                        "                            <td>"+articleinfo[i].title+"</td>\n" +
                        "                            <td><span class='label label-purple'>"+articleinfo[i].categoriesName+"</span></td>\n" +
                        "                            <td>"+articleinfo[i].time+"</td>";
                    if(articleinfo[i].status==1){
                        s1+="<td><span class=\"label label-info\">已发布</span></td>";
                    }else
                        s1+="<td><span class=\"label label-danger\">未发布</span></td>";
                    if(articleinfo[i].tagsName.length>0){
                        s1+="<td>";
                        for (var j = 0; j < articleinfo[i].tagsName.length; j++) {
                            s1+="<span class=\"label label-info\">"+articleinfo[i].tagsName[j]+"</span>&nbsp;"
                        }
                        s1+="</td>";
                    }
                    else
                    s1+="<td></td>";
                    s1+="<td><a href=\"/detail/"+articleinfo[i].date+"/"+articleinfo[i].description+"\" class=\"btn btn-default\">查看</a>&nbsp;<button class='btn btn-danger' onclick='deletearticle("+articleinfo[i].id+")'>删除</button>&nbsp;<a href=\"/admin/article/edit/"+articleinfo[i].description+"\" class='btn btn-primary'>编辑</a></td>";
                }
            }
            var pageinfo=res.data.pageinfo;
            $("#current-page").html("第"+pageinfo.pageNum+"页");
            if(!pageinfo.hasPreviousPage){
                $("#first").attr("class","disabled");
                $("#pre").attr("class","disabled");
                $("#first").html("<a href=\"javascript:void(0);\">首页</a>");
                $("#pre").html("<a href=\"javascript:void(0);\" >上一页</a>");
            }else{
                $("#first").removeAttr("class","disabled");
                $("#pre").removeAttr("class","disabled");
                $("#first").html("<a href=\"javascript:void(0);\" onclick=\"getinfo(1)\">首页</a>");
                $("#pre").html("<a href=\"javascript:void(0);\" onclick=\"getinfo("+pageinfo.prePageNum+")\">上一页</a>");
            }
            if(!pageinfo.hasNextPage){
                $("#next").attr("class","disabled");
                $("#last").attr("class","disabled");
                $("#next").html("<a href=\"javascript:void(0);\">下一页</a>");
                $("#last").html("<a href=\"javascript:void(0);\" >尾页</a>");
            }else{
                $("#next").removeAttr("class","disabled");
                $("#last").removeAttr("class","disabled");
                $("#next").html("<a href=\"javascript:void(0);\" onclick=\"getinfo("+pageinfo.nextPageNum+")\">下一页</a>");
                $("#last").html("<a href=\"javascript:void(0);\" onclick=\"getinfo("+pageinfo.totalpage+")\">尾页</a>");
            }
            $("#total").html(pageinfo.totalpage);
            $("#tbody").html(s1);
        }
    })
}
function transferto() {
    var tale=new $.tale();
    var pagenum=$("#pagenum").val();
    var totalpage=$("#total").html();
    if(pagenum<1||pagenum>totalpage||pagenum==''){
        tale.alertWarn("页码无效!");
        return;
    }
    getinfo(pagenum);
}
function deletearticle(id) {
    swal({
        title: '确定删除吗?',
        type: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#3085d6',
        cancelButtonColor: '#d33',
        confirmButtonText: '确定',
        cancelButtonText:"取消",
    }).then(function(isConfirm) {
        if (isConfirm) {
            $.post({
                xhrFields: {
                    withCredentials: true
                },
                url: baseurl+api.article.delete+id,
                success:function (res) {
                    if(res.code=="200"){
                        swal('删除成功!','','success');
                        setTimeout(function () {
                            window.location.reload();
                        },2000);
                    }else
                        swal('删除失败');
                }
            })
        }
    })
}
